package com.zc.controller;
import com.zc.component.EntityFile;
import com.zc.entity.Fileer;
import com.zc.entity.Filepath;
import com.zc.mapper.FileerMapper;
import com.zc.mapper.FilepathMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
public class UploadController {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    EntityFile entityFile;
    @Autowired
    FilepathMapper filepathMapper;
    @Autowired
    FileerMapper fileerMapper;
    /*
     * 获取file.html页面
     */
    @RequestMapping("file")
    public String file(Model model) {
        model.addAttribute("filepath",filepathMapper.selectAll());
        return "file/fileup";
    }

    /**
     * 实现文件上传
     */
    @RequestMapping("fileUpload")
    @ResponseBody
    public String fileUpload(@RequestParam("fileName") MultipartFile file, Fileer fileer,
                             HttpSession session) {
        int teamid=(int)session.getAttribute("teamid");
        Fileer fileer1 = fileerMapper.selectByNameAndTeamIdAndSenheAndFilepath(file.getOriginalFilename(), teamid, fileer.getFilepath(), "0");
        if(fileer1!=null)
        {
            return "文件正在被操作请稍后重试";
        }
        logger.info(fileer.toString());
        fileer.setName(file.getOriginalFilename());
        fileer.setFlag("0");
        fileer.setTeamid((int)session.getAttribute("teamid"));
        fileer.setUp(new Date());
        fileer.setUpname((String)session.getAttribute("loginUser"));
        fileer.setSenhe(0);
        if (file.isEmpty()) {
            return "false";
        }
        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        System.out.println(fileName + "-->" +size);

        String path = entityFile.getBetweenRepository();
        File dest = new File(path +teamid+"/"+ fileName);
        if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        try {
            fileerMapper.insertSelective(fileer);
            file.transferTo(dest); //保存文件

            return "操作成功";
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return "false";
        }
    }


    @ResponseBody
    @GetMapping("/shfilelist")
    public List  shfile(HttpSession session)
    {
        int teamid =(int)session.getAttribute("teamid");
        return fileerMapper.selectByTeamIdAndSH(teamid);
    }
}
/*　

    *//**
     * 实现多文件上传
     * *//*
    @RequestMapping(value="multifileUpload",method= RequestMethod.POST)
　　*//**public @ResponseBody String multifileUpload(@RequestParam("fileName")List<MultipartFile> files) *//*
    public @ResponseBody String multifileUpload(HttpServletRequest request){

        List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("fileName");

        if(files.isEmpty()){
            return "false";
        }

        String path = "F:/test" ;

        for(MultipartFile file:files){
            String fileName = file.getOriginalFilename();
            int size = (int) file.getSize();
            System.out.println(fileName + "-->" + size);

            if(file.isEmpty()){
                return "false";
            }else{
                File dest = new File(path + "/" + fileName);
                if(!dest.getParentFile().exists()){ //判断文件父目录是否存在
                    dest.getParentFile().mkdir();
                }
                try {
                    file.transferTo(dest);
                }catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    return "false";
                }
            }
        }
        return "true";
    }
}


*/
























































































